﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DTO;
namespace EC21_EDiamond
{
    public partial class QuanLyKhachHang1 : System.Web.UI.Page
    {
        KhachHangBUS khbus = new KhachHangBUS();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                LoadDanhSachKhachHang();
                LoadDanhSachTinh();
            }
        }
        void LoadDanhSachTinh()
        {
            TinhBUS tinhbus = new TinhBUS();
            drpTinh.DataSource = tinhbus.LoadDanhSachTinh();
            drpTinh.DataTextField = "TenTinh";
            drpTinh.DataValueField = "MaTinh";
            drpTinh.DataBind();
        }
        void LoadDanhSachKhachHang()
        {
            grwDanhSachKhachHang.DataSource= khbus.LoadDanhSachKhachHang();
            grwDanhSachKhachHang.DataBind();
        }
        protected void btnCapNhat_Click(object sender, EventArgs e)
        {
            if (makh == 0)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Chưa Chọn Khách Hàng!');", true);
                return;
            }
            KhachHangDTO kh = new KhachHangDTO();
            kh.MaKH = makh;
            kh.TenKH = txtTenUser.Text;
            kh.DiaChi = txtDiaChi.Text;
            kh.MaTinh = int.Parse(drpTinh.SelectedValue);
            kh.SoDT = txtDienThoai.Text;
            kh.Email = txtEmail.Text;
            kh.Tinhtrang = int.Parse(drpTinhTrang.SelectedValue.ToString());
            if (khbus.CapNhatKhachHang(kh) != 0)
            {
                LoadDanhSachKhachHang();
                txtTaiKhoan.Text = "";
                txtTenUser.Text = "";
                txtDiaChi.Text = "";
                txtDienThoai.Text = "";
                txtEmail.Text = "";
                makh = 0;
                txtTaiKhoan.ReadOnly = false;
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Cập Nhật Thành Công!');", true);             
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Cập Nhật Thất Bại!');", true);
            }
        }
        static int makh = 0;
        protected void grwDanhSachKhachHang_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            txtTaiKhoan.ReadOnly = true;
            makh = int.Parse(grwDanhSachKhachHang.Rows[e.NewSelectedIndex].Cells[1].Text);
            txtTaiKhoan.Text = grwDanhSachKhachHang.Rows[e.NewSelectedIndex].Cells[3].Text;
            txtTenUser.Text = HttpUtility.HtmlDecode(grwDanhSachKhachHang.Rows[e.NewSelectedIndex].Cells[2].Text);
            txtDiaChi.Text = HttpUtility.HtmlDecode(grwDanhSachKhachHang.Rows[e.NewSelectedIndex].Cells[4].Text);
            txtDienThoai.Text = grwDanhSachKhachHang.Rows[e.NewSelectedIndex].Cells[5].Text;
            txtEmail.Text = grwDanhSachKhachHang.Rows[e.NewSelectedIndex].Cells[6].Text;
            drpTinh.SelectedValue = grwDanhSachKhachHang.Rows[e.NewSelectedIndex].Cells[7].Text;
            drpTinhTrang.SelectedValue = grwDanhSachKhachHang.Rows[e.NewSelectedIndex].Cells[8].Text;
        }

        protected void btnTimKiem_Click(object sender, EventArgs e)
        {
            KhachHangBUS khbus = new KhachHangBUS();
            grwDanhSachKhachHang.DataSource = khbus.TimKiemKhachHang(txtTaiKhoan.Text);
            grwDanhSachKhachHang.DataBind();
        }
 
       
    }
}